package de.unidue.masterproject.plugin;

import java.util.List;

import de.unidue.masterproject.ccc.exceptions.P2PCommunicationException;
import de.unidue.masterproject.ccc.exceptions.WrongValueException;
import de.unidue.masterproject.generalinterfaces.IAccessDAO;
import de.unidue.masterproject.generalmodel.P2PCommunicationObject;
import de.unidue.masterproject.generalplugin.GeneralPlugin;
import de.unidue.masterproject.generalplugin.PlugInMetaInfo;





public class Plugin2 extends GeneralPlugin {

	public Plugin2(){
		System.out.println("Plugin2 geladen");
	}

	@Override
	public void run(boolean firstStart){
		
		myFrames[0].setTitle("Fenster 1, Plug-In2");
		
		
		for(int i = 0; i<myFrames.length;i++){
			myFrames[i].setBounds(0,50,200,200);
			myFrames[i].show();
		}
		
		
		try {
			p2pCS.sendObject(new P2PCommunicationObject("LiteratureManagemementPlugin", 0, false, false, "Nachricht1 vom Plugin2"), this);
		} catch (P2PCommunicationException e1) {
			System.out.println("Kommunikationsfehler: "+e1.getMessage());
		}
		
		try {
			p2pCS.sendObject(new P2PCommunicationObject("LiteratureManagemementPlugin", 5, false, false, "Nachricht2 vom Plugin2, die nicht ankommen darf!"), this);
		} catch (P2PCommunicationException e1) {
			System.out.println("Kommunikationsfehler: "+e1.getMessage());
		}
		
		try {
			p2pCS.sendObject(new P2PCommunicationObject(null, 0, true, false, "Broadcast-Nachricht3 vom LVP"), this);
		} catch (P2PCommunicationException e1) {
			System.out.println("Kommunikationsfehler: "+e1.getMessage());
		}
		
		
		
		
		try {
			
			
			
			IAccessDAO<SomeRow> accessDAO = (IAccessDAO<SomeRow>) getAccessDAO(SomeRow.class); 
			
			if(!accessDAO.correspondingDBTableExists()){
				accessDAO.createCorrespondingDBTable();
			}

			
			SomeRow s1 = new SomeRow();
			s1.setFeld("z");
			s1.setWeiteres("z");
			s1.setId(4);
			
			SomeRow s2 = new SomeRow();
			s2.setFeld("y");
			s2.setWeiteres("y");
			s2.setId(3);
			
			SomeRow[] srs = {s2,s1};
			
			 //System.out.println(accessDAO.saveObjects(srs, false));
			
			List<SomeRow> results = (List<SomeRow>) accessDAO.selectAll();
			
			for(int i = 0;i<results.size();i++){
	        	SomeRow sr = results.get(i);
	        	System.out.println(sr.getId()+"; "+sr.getFeld()+"; "+sr.getWeiteres());
	        }

		
			
			
			
			
			//accessDAO.saveObjects(sr);
			
			accessDAO.disconnect();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void createMetaInfo() {
		String[] s = {"nichts :-("};
		Class<?>[] c = {SomeRow.class};
		String[] namesOfRequiredTables = {"somerow"};
		try {
			plugInMetaInfo = new PlugInMetaInfo("Plugin2", 0.1, "Ich" ,1, true, false, false, false, true, s, c, namesOfRequiredTables);
		} catch (WrongValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void receiveP2PObject(String senderName, double senderVersion,
			P2PCommunicationObject p2pCO) throws P2PCommunicationException {
		System.out.println("Nachricht von "+senderName+"("+senderVersion+") in Plugin2 erhalten:\n"+(String)p2pCO.getMessageObject());
		
	}

	@Override
	protected void terminate() {
		myFrames[0].hide();
		
	}

	@Override
	public void performUpdate(double oldVersion) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prepareUpdate(double oldVersion) {
		// TODO Auto-generated method stub
		
	}
		
}
